#include <iostream>
#include <vector>
#include <algorithm>

using std::cout;
using std::endl;
using std::vector;
using std::remove_if;
using std::for_each;

bool func(int value)
{
    return value > 4;
}

void test()
{
    vector<int> vec = {1, 5, 3, 2, 6, 9, 8, 4};
    for_each(vec.begin(), vec.end(), [](int value){
             cout << value << "  ";
             });
    cout << endl;

    cout << endl << "将所有大于4的元素删除" << endl;
    //注意：remove_if函数不能一次将满足条件的元素删除，但是会返回待删除
    //元素的首迭代器，然后再配合容器的erase就可以将多余的元素删除
    auto it = remove_if(vec.begin(), vec.end(), func);
    vec.erase(it, vec.end());
    for_each(vec.begin(), vec.end(), [](int value){
             cout << value << "  ";
             });
    cout << endl;
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

